<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/2
 * Time: 15:00
 */

namespace app\sports_admin\model;


use app\exception\SchoolNoticeException;
use think\Model;
use think\Request;
use app\sports_admin\model\Student;
use think\Db;
use app\exception\TrainException;
use traits\model\SoftDelete;

class SchoolNotice extends Model
{
    use SoftDelete;
    protected $table = "notice";
    protected $pk    = "id";
    protected $autoWriteTimestamp = true;

    public function getContentsAttr($value)
    {
        return htmlspecialchars_decode($value);
    }
    public function getSchool()
    {
        return $this->belongsTo("School","school_id","school_id");
    }

    /**学校公告列表
     * @param $page
     * @param $rows
     * @param $sort
     * @param $sortOrder
     * @param $searchName 文件名
     * @param $school 学校id
     * @return \think\response\Json
     */
    public static function noticeList($page,$rows,$sort,$sortOrder,$searchName,$school)
    {
        $result = self::with("getSchool")
            ->where(function($query)use($searchName,$school){
                if($searchName){
                    $query->where('title','like',"%{$searchName}%");
                }
                if($school){
                    $query->where('school_id',$school);
                }
                $query->where('school_id','<>',0);
            })->order("{$sort}","{$sortOrder}")
            ->page($page)->limit($rows)->select();
        $count = self::with("getSchool")
            ->where(function($query)use($searchName,$school){
                if($searchName){
                    $query->where('title','like',"%{$searchName}%");
                }
                if($school){
                    $query->where('school_id',$school);
                }
                $query->where('school_id','<>',0);
            })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }

    /**
     * 添加学校公告列表
     */
    public static function noticeAdd()
    {
        $data = Request::instance()->only('school_id,name,content');
        $content = htmlspecialchars($data['content']);
        $type = $data['school_id'] == 0 ? 2 : 1;
        $id = SchoolNotice::create(['school_id'=>$data['school_id'],'title'=>$data['name'],'contents'=>$content,'release_time'=>time(),'type'=>$type]);
        if(!$id) {
            throw new SchoolNoticeException([
                'msg' => '添加失败',
                'errorCode' => 400
            ]);
        }
        return json(['code'=>202,'msg'=>'添加成功']);
    }
//    /**
//     * 删除学校公告
//     */
//    public function noticeDel()
//    {
//        $data =  Request::instance()->only('id');
//        Db::startTrans();
//        $del_notice = $this->where('id',$data['id'])->delete();
//        $del_read   = IsReadNotice::where('notice_id',$data['id'])->delete();
//        if($del_read && $del_notice){
//            Db::commit();
//            return json(['code'=>202,'msg'=>'删除成功']);
//        }else{
//            Db::rollback();
//            throw new SchoolNoticeException([
//                'msg'=>'删除失败',
//                'errorCode'=>400
//            ]);
//        }
//    }
    /**
     * 更新学校公告
     */
    public static function noticeUpdate()
    {
        $data = Request::instance()->only('id,title,content');
        $time = time();
        $content = htmlspecialchars($data['content']);
        return self::where('id',$data['id'])->update(['title'=>$data['title'],'update_time'=>$time,'contents'=>$content]);
    }
}